package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.BatchItemType;
import com.edu.exam.enums.BatchItemStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 批量操作项目实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：batch_operation_items
 * 对应数据库表：batch_operation_items
 *
 * 记录批量操作中每个具体项目的处理详情，包括项目状态、处理结果、错误信息等。
 * 支持细粒度的批量操作管理。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("批量操作项目表")
@SQLDelete(sql = "UPDATE batch_operation_items SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class BatchOperationItem {

    /**
     * 项目记录唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 批量操作ID
     */
    @NotNull(message = "批量操作ID不能为空")
    @Column(name = "batch_id", nullable = false)
    private Long batchId;

    /**
     * 项目类型
     */
    @NotNull(message = "项目类型不能为空")
    @Column(name = "item_type", nullable = false, length = 20)
    private String itemType;

    /**
     * 原始项目ID
     */
    @NotNull(message = "原始项目ID不能为空")
    @Size(max = 50, message = "原始项目ID长度不能超过50个字符")
    @Column(name = "item_id_original", nullable = false, length = 50)
    private String itemIdOriginal;

    /**
     * 项目名称
     */
    @Size(max = 200, message = "项目名称长度不能超过200个字符")
    @Column(name = "item_name", length = 200)
    private String itemName;

    /**
     * 项目数据，JSON格式
     */
    @Column(name = "item_data", columnDefinition = "TEXT")
    private String itemData;

    /**
     * 项目状态
     */
    @Column(name = "status", nullable = false, length = 20)
    private String status;

    /**
     * 结果数据，JSON格式
     */
    @Column(name = "result_data", columnDefinition = "TEXT")
    private String resultData;

    /**
     * 错误信息
     */
    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    /**
     * 错误代码
     */
    @Size(max = 50, message = "错误代码长度不能超过50个字符")
    @Column(name = "error_code", length = 50)
    private String errorCode;

    /**
     * 重试次数
     */
    @Column(name = "retry_count")
    @Builder.Default
    private Integer retryCount = 0;

    /**
     * 最大重试次数
     */
    @Column(name = "max_retry_count")
    @Builder.Default
    private Integer maxRetryCount = 3;

    /**
     * 处理时间
     */
    @Column(name = "process_time")
    private LocalDateTime processTime;

    /**
     * 处理耗时（毫秒）
     */
    @Column(name = "process_duration")
    private Long processDuration;

    /**
     * 处理优先级
     */
    @Column(name = "process_priority")
    @Builder.Default
    private Integer processPriority = 0;

    /**
     * 处理服务器
     */
    @Size(max = 100, message = "处理服务器长度不能超过100个字符")
    @Column(name = "process_server", length = 100)
    private String processServer;

    /**
     * 处理人
     */
    @Size(max = 50, message = "处理人长度不能超过50个字符")
    @Column(name = "processor", length = 50)
    private String processor;

    /**
     * 处理批次
     */
    @Size(max = 50, message = "处理批次长度不能超过50个字符")
    @Column(name = "process_batch", length = 50)
    private String processBatch;

    /**
     * 处理结果摘要
     */
    @Size(max = 500, message = "处理结果摘要长度不能超过500个字符")
    @Column(name = "process_summary", length = 500)
    private String processSummary;

    /**
     * 项目标签
     */
    @Size(max = 200, message = "项目标签长度不能超过200个字符")
    @Column(name = "item_tags", length = 200)
    private String itemTags;

    /**
     * 项目元数据，JSON格式
     */
    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    /**
     * 记录创建时间
     */
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 记录最后更新时间
     */
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 最后更新人用户ID
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createTime = now;
        this.updateTime = now;
        if (this.status == null) {
            this.status = BatchItemStatus.PENDING.getCode();
        }
    }

    @PreUpdate
    protected void onUpdate() {
        this.updateTime = LocalDateTime.now();
    }


}